package com.xhd.server;

import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Vector;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.PropertyResourceBundle;
import java.util.Vector; //import javax.xml.marshal.XMLWriter;
import javax.xml.marshal.XMLWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.xhd.base.log.LogFactory;
import com.xhd.util.DataRelation;
import com.xhd.xml.XMLSerializer;

public class SysConfigFile {
	private static SysConfigFile configins = null;
	static final String jm234 = "-refs";
	private Hashtable htconfig = new Hashtable();
	static Class insobj;
	private static Class<?> jm10;

	public static SysConfigFile getInstance() {
		if (configins == null)
			configins = new SysConfigFile();
		return configins;
	}

	private SysConfigFile() {
		if (configins != null)
			return;
		jm231();
		if ((!(this.htconfig.containsKey("install")))
				|| (!("true".equalsIgnoreCase((String) this.htconfig
						.get("install")))))
			return;
		jm232();
	}

	private void jm231() {
		FileInputStream localFileInputStream = null;
		DocumentBuilderFactory localDocumentBuilderFactory = null;
		DocumentBuilder localDocumentBuilder = null;
		InputSource localInputSource = null;
		Document localDocument = null;
		try {
			String str1 = ServerConfig.getConfFilePath("config");
			localFileInputStream = new FileInputStream(str1);
			String str2 = ServerConfig.getConfFilePath("license.cpc");
			Object localObject1 = new File(str2);
			Object localObject2 = new XMLSerializer(localFileInputStream, false);
			String str3 = ((XMLSerializer) localObject2).getData("licence");

			localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
			localDocumentBuilder = localDocumentBuilderFactory
					.newDocumentBuilder();
			localInputSource = new InputSource(localFileInputStream);
			localDocument = localDocumentBuilder.parse(localInputSource);
			Element localElement = localDocument.getDocumentElement();
			NodeList localNodeList1 = localElement.getChildNodes();
			for (int i = 0; i < localNodeList1.getLength(); ++i) {
				localObject1 = localNodeList1.item(i);
				localObject2 = ((Node) localObject1).getNodeName()
						.toLowerCase();
				str3 = "";
				if (((String) localObject2).equals("#comment"))
					continue;
				if (((String) localObject2).equals("#text"))
					continue;
				NodeList localNodeList2;
				Object localObject3;
				Object localObject4;
				if (((String) localObject2).indexOf("-refs") != -1) {
					localNodeList2 = ((Node) localObject1).getChildNodes();
					DataRelation localDataRelation = new DataRelation("data");
					for (int k = 0; k < localNodeList2.getLength(); ++k) {
						localObject3 = localNodeList2.item(k);
						localObject4 = ((Node) localObject3).getChildNodes();
						Hashtable localHashtable = new Hashtable();
						for (int l = 0; l < ((NodeList) localObject4)
								.getLength(); ++l) {
							Node localNode3 = ((NodeList) localObject4).item(l);
							String str4 = localNode3.getNodeName()
									.toLowerCase();
							String str5 = "";
							if (str4.equals("#comment"))
								continue;
							if (str4.equals("#text"))
								continue;
							NodeList localNodeList3 = localNode3
									.getChildNodes();
							Node localNode4 = localNodeList3.item(0);
							if (localNode4 != null)
								str5 = localNode4.getNodeValue();
							localHashtable.put(str4, str5);
						}
						if (localHashtable.size() <= 0)
							continue;
						localDataRelation.getRelationData().addElement(
								localHashtable);
					}
					this.htconfig.put(localObject2, localDataRelation);
				} else if (((String) localObject2).indexOf("-ref") != -1) {
					localNodeList2 = ((Node) localObject1).getChildNodes();
					for (int j = 0; j < localNodeList2.getLength(); ++j) {
						Node localNode2 = localNodeList2.item(j);
						localObject2 = localNode2.getNodeName().toLowerCase();
						str3 = "";
						if (((String) localObject2).equals("#comment"))
							continue;
						if (((String) localObject2).equals("#text"))
							continue;
						localObject3 = localNode2.getChildNodes();
						localObject4 = ((NodeList) localObject3).item(0);
						if (localObject4 != null)
							str3 = ((Node) localObject4).getNodeValue();
						this.htconfig.put(localObject2, str3);
					}
				} else {
					localNodeList2 = ((Node) localObject1).getChildNodes();
					Node localNode1 = localNodeList2.item(0);
					if (localNode1 != null)
						str3 = localNode1.getNodeValue();
					this.htconfig.put(localObject2, str3);
				}
			}
			localFileInputStream.close();
			str2 = ServerConfig.getConfFilePath("license.cpc");
			localObject1 = new File(str2);
			if (!(((File) localObject1).exists()))
				return;
			localFileInputStream = new FileInputStream(str2);
			localObject2 = new XMLSerializer(localFileInputStream, false);
			str3 = ((XMLSerializer) localObject2).getData("licence");
			if ((str3 == null) || (str3.length() <= 0))
				return;
			this.htconfig.put("licence", str3);
		} catch (Throwable localThrowable) {
			localThrowable.printStackTrace();
		}
	}

	private void jm232() {
		String str1 = ServerConfig.getConfFilePath("install.xml");
		File localFile1 = new File(str1);
		if ((!(localFile1.exists())) || (!(localFile1.isFile())))
			return;
		FileInputStream localFileInputStream = null;
		try {
			localFileInputStream = new FileInputStream(localFile1);
			PropertyResourceBundle localPropertyResourceBundle = new PropertyResourceBundle(
					localFileInputStream);
			Enumeration localEnumeration = localPropertyResourceBundle
					.getKeys();
			while (localEnumeration.hasMoreElements()) {
				String str2 = (String) localEnumeration.nextElement();
				String str3 = localPropertyResourceBundle.getString(str2);
				int i = str2.indexOf(47);
				if (i > 0) {
					String str4 = str2.substring(0, i).toLowerCase();
					str2 = str2.substring(i + 1);
					if (!(str4.endsWith("-refs")))
						str4 = str4 + "-refs";
					DataRelation localDataRelation = (DataRelation) this.htconfig
							.get(str4);
					if (localDataRelation == null)
						continue;
					Vector localVector = localDataRelation.getRelationData();
					if ((localVector == null) || (localVector.size() <= 0))
						continue;
					for (int j = 0; j < localVector.size(); ++j) {
						Hashtable localHashtable = (Hashtable) localVector
								.get(j);
						localHashtable.put(str2, str3);
					}
				} else {
					this.htconfig.put(str2, str3);
				}
			}
		} catch (Exception localException1) {
			localException1.printStackTrace();
		} finally {
			if (localFileInputStream != null)
				try {
					localFileInputStream.close();
				} catch (Exception localException2) {
				}
		}
		this.htconfig.remove("install");
		saveConfig();
		File localFile2 = new File(localFile1.getPath() + ".ok");
		if (localFile2.exists())
			localFile2.delete();
		localFile1.renameTo(localFile2);
	}

	public void saveConfig() {
		FileOutputStream localFileOutputStream = null;
		XMLWriter localXMLWriter = null;
		int i = 0;
		try {
			String str1 = ServerConfig.getConfFilePath("config");
			localFileOutputStream = new FileOutputStream(str1);
			localXMLWriter = new XMLWriter(localFileOutputStream, "UTF-16");
			localXMLWriter.start("xhd.com");
			Enumeration localEnumeration = this.htconfig.keys();
			while (localEnumeration.hasMoreElements()) {
				String str2 = localEnumeration.nextElement().toString();
				if (str2.indexOf("-refs") != -1) {
					DataRelation localDataRelation = (DataRelation) this.htconfig
							.get(str2);
					jm233(str2, localDataRelation, localXMLWriter);
				} else {
					if (str2.length() <= 0)
						continue;
					localXMLWriter.leaf(str2, (String) this.htconfig.get(str2));
				}
			}
			localXMLWriter.end("xhd.com");
			localXMLWriter.flush();
		} catch (Exception localException) {
			localException.printStackTrace();
		} catch (Throwable localThrowable) {
			localThrowable.printStackTrace();
		} finally {
			try {
				localXMLWriter.close();
				localFileOutputStream.close();
			} catch (IOException localIOException) {
				Class tmp199_196 = null;
				if (tmp199_196 == null) {

					try {
						SysConfigFile.jm10 = Class
								.forName("com.sinocc.base.config.SysConfigFile");
					} catch (ClassNotFoundException localClassNotFoundException) {
						throw new NoClassDefFoundError(
								localClassNotFoundException.getMessage());
					}
				}
				LogFactory.getLog(tmp199_196).error(localIOException);
				if (i == 0)
					localIOException.printStackTrace();
			}
		}
	}

	private void jm233(String paramString, DataRelation paramDataRelation,
			XMLWriter paramXMLWriter) {
		try {
			paramXMLWriter.start(paramString);
			if (paramDataRelation != null) {
				String str1 = paramDataRelation.getRelationName();
				if ((paramDataRelation.getRelationData() != null)
						&& (paramDataRelation.getRelationData().size() != 0))
					for (int i = 0; i < paramDataRelation.getRelationData()
							.size(); ++i) {
						Hashtable localHashtable = (Hashtable) paramDataRelation
								.getRelationData().elementAt(i);
						if ((localHashtable == null)
								|| (localHashtable.size() == 0))
							continue;
						paramXMLWriter.start(str1);
						Enumeration localEnumeration = localHashtable.keys();
						while (localEnumeration.hasMoreElements()) {
							String str2 = (String) localEnumeration
									.nextElement();
							try {
								String str3 = (String) localHashtable.get(str2);
								paramXMLWriter.leaf(str2, str3);
							} catch (Exception localException2) {
								DataRelation localDataRelation = null;
								try {
									localDataRelation = (DataRelation) localHashtable
											.get(str2);
								} catch (Exception localException3) {
									localException3.printStackTrace();
								}
								if (localDataRelation != null)
									jm233(str2, localDataRelation,
											paramXMLWriter);
							}
						}
						paramXMLWriter.end(str1);
					}
			}
			paramXMLWriter.end(paramString);
		} catch (Exception localException1) {
			localException1.printStackTrace();
		} catch (Throwable localThrowable) {
			localThrowable.printStackTrace();
		}
	}

	public int getInt(String paramString, int paramInt) {
		int i = paramInt;
		String str = (String) this.htconfig.get(paramString);
		if ((str != null) && (str.length() > 0))
			i = Integer.parseInt(str);
		return i;
	}

	public int getInt(String paramString) {
		int i = 0;
		String str = (String) this.htconfig.get(paramString);
		if ((str != null) && (str.length() > 0))
			i = Integer.parseInt(str);
		return i;
	}

	public boolean getBoolean(String paramString, boolean paramBoolean) {
		boolean bool = paramBoolean;
		String str = (String) this.htconfig.get(paramString);
		if ((str != null) && (str.length() > 0))
			bool = "true".equalsIgnoreCase(str);
		return bool;
	}

	public boolean getBoolean(String paramString) {
		boolean bool = false;
		String str = (String) this.htconfig.get(paramString);
		if ((str != null) && (str.length() > 0))
			bool = "true".equalsIgnoreCase(str);
		return bool;
	}

	public String getString(String paramString1, String paramString2) {
		String str = (String) this.htconfig.get(paramString1);
		if (str != null)
			return str;
		return paramString2;
	}

	public String getString(String paramString) {
		return ((String) this.htconfig.get(paramString));
	}

	public Vector getVector(String paramString) {
		if (paramString != null) {
			paramString = paramString.toLowerCase();
			if (!(paramString.endsWith("-refs")))
				paramString = paramString + "-refs";
			Object localObject = this.htconfig.get(paramString);
			if (localObject != null) {
				if (localObject instanceof DataRelation)
					return ((DataRelation) localObject).getRelationData();
				return ((Vector) localObject);
			}
		}
		return null;
	}

	public Hashtable getConfigs() {
		return ((Hashtable) this.htconfig.clone());
	}
}
